package DTO;

import java.util.ArrayList;

import database.DatabaseHelper;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class ChapterDTO {
	private String chapter;
	private String name;
	
	//Property Chapter
	public String getChapter() { return chapter; }
	public void setChapter(String chapter) { this.chapter = chapter;}
	
	//Property Name
	public String getName() { return name; }
	public void setName(String name) { this.name = name; }
	
	public static ArrayList<ChapterDTO> getAllChapter(Context context){
		ArrayList<ChapterDTO> result = new ArrayList<ChapterDTO>();
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	String query = "SELECT * FROM Chapters";
	 	Cursor cursor = null;
	 	try{
	 		cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
	 	}catch(Exception e){
	 		
	 	}
	 	if(cursor.moveToFirst()){
	 		do{
	 			ChapterDTO chapterDTO = new ChapterDTO();
	 			chapterDTO.setChapter(cursor.getString(cursor.getColumnIndex("Chapter")));
	 			chapterDTO.setName(cursor.getString(cursor.getColumnIndex("Name")));
	 			result.add(chapterDTO);
	 		}while(cursor.moveToNext());
	 	}
	 	myDbHelper.close();
	 	return result;
	}
	
	public static ChapterDTO getChapter(Context context, String chapter){
		ChapterDTO result = new ChapterDTO();
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	String query = "SELECT * FROM Chapters WHERE Chapter = '" + chapter + "'";
	 	Cursor cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
	 	if(cursor.moveToFirst()){
	 		result.setChapter(cursor.getString(cursor.getColumnIndex("Chapter")));
	 		result.setName(cursor.getString(cursor.getColumnIndex("Name")));
	 	}
	 	myDbHelper.close();
	 	return result;
	}
}
